$(function() {

// 曲谱
$('.melody1').css({'height':$(document).height()/1.8+'px'});
$('.melody2').css({'height':$(document).height()/1.8+'px'});
$('.play').click(function() {
  $('.melody1').css({'background-image':'url("'+$(this).children(":first").html()+'")'});
    $('.melody2').css({'background-image':'url("'+$(this).children(":last").html()+'")'});
});
$('.stop').click(function() {
    $('.melody1').css({'background-image':'url("../images/first1.png")'});
    $('.melody2').css({'background-image':'url("../images/first2.png")'});
});
$('.moreHeader').css({
  'font-size':'40px',
  'font-family':'幼圆',
  'color':'white'
});

// 追加钢琴
  pianoMusic();
  function pianoMusic() {
    for (var i = 1; i <= 36; i++) {
      $('.piano').append('<video id="pianom'+i+'" src="../audio/piano/'+i+'.mp3" oncanplaythrough="musicprograss()"></video>');
      $('#pianom'+i)[0].volume = 0.5;
    }
  };

//加载
$('.loading').css({
  'height':$(document).height()+'px',
  'width':$(document).width()+'px'
});

//音量
$('.sound').css({
  'width':$('.sound').parent().height()*0.8+'px'
})

$('.sound').mousemove(function() {
  var sound = $(this).children().val()/100;
  for (var i = 1; i <= 36; i++) {
    $('#pianom'+i)[0].volume = sound;
  }
})

//钢琴
  $('.pianoKeys').mouseover(function() {
    var nums = $(this).attr('id');
    var num = nums.substring(3);
    pianodown(num);
  });

  $('.pianoKeys').mouseout(function() {
    var ids = $(this).attr('id');
    var id = '#'+ids;
    pianoup(id);
  });

  var m1;
  var m2;

  $('.pianoKeys').on('touchstart',function(e) {
    m1=e.screenX;
    var nums = $(this).attr('id');
    var num = nums.substring(3);
    pianodown(num);
  });

  $('.pianoKeys').on('touchmove',function(e) {
    m2=e.screenX;
    if(m2-m1>$('.pianoKeys').width()) {
       var nums = $(this).attr('id');
       var num = nums.substring(3);
       pianodown(num);
    }
  });

  $('.pianoKeys').on('touchend',function() {
    var ids = $(this).attr('id');
    var id = '#'+ids;
    pianoup(id);
  });
// 说明
$('.introduce').click(function() {
  $('#introduce').css({
    '-webkit-transform':'scale(1)',
    'transform':'scale(1)'
  });
  $('#introduces').css({
    '-webkit-transform':'scale(1)',
    'transform':'scale(1)'
  });
});
$('#introduces').click(function() {
  $('#introduce').css({
    '-webkit-transform':'scale(0)',
    'transform':'scale(0)'
  });
  $(this).css({
    '-webkit-transform':'scale(0)',
    'transform':'scale(0)'
  });
});
// 更多
var playmoreid;
var CF =3;
$('.moremusic').css({
  'width': $(window).width()/1.2+'px',
  'height': $(window).height()/1.2+'px'
});

$('.playmore').click(function() {
  CF = 3;
  $('.moremusic').css({
    'transform': 'scale(1)'
  });
  playmoreid = $(this).attr('id')
  moremusic();
});

$('.closemore').click(function() {
  $('.moremusic').css({
    'transform': 'scale(0)'
  });
});

$(document).on('click','.singersong',function(){
  $('#'+playmoreid).prev().children(':first').html($(this).children(':first').html());
  $('#'+playmoreid).prev().children(':last').html($(this).children(':last').html());
  var str = $(this).prev().attr('style');
  str = str.substring(21,str.length-2);
  $('#'+playmoreid).parent().prev().attr('src',str);
  var singername = $(this).children(':first').next().html();
  $('#'+playmoreid).parent().children(':first').html(singername);
  $('#'+playmoreid).parent().parent().parent().parent().prev().children().children().html(singername);
  $('.melody1').css({'background-image':'url("'+$(this).children(':first').html()+'")'});
  $('.melody2').css({'background-image':'url("'+$(this).children(':last').html()+'")'});
  $('.moremusic').css({
    'transform': 'scale(0)'
  });
})

$('.moreTable').on('scroll',function() {
  var FT = $('.moreTable').position().top;
  var CT = $('.morelist').position().top;
  var FH = $('.moreTable').height();
  var CH = $('.morelist').height();
  if((FT-CT)==Math.floor(CH-FH)) {
      $('.onload').css({
        'display':'block'
      })
      setTimeout(function() {
        moreandmore(CF);
        CF++;
        setTimeout(function() {
          $('.onload').css({
            'display':'none'
          },5000);
        })
      },1000);
  }
})

// 搜索
$('.search').click(function() {
  var snames = $('#exampleInputEmail2').val();
  for(var i = 0;i<$('.moresinger').length;i++) {
    if(snames == $($('.moresinger')[i]).html()) {
      var rsn = $($('.moresinger')[i]).attr('rsn');
      searchsinger(rsn);
      $('.nofill').css({
        'display':'none'
      })
      break;
    }
    else {
      $('.nofill').css({
        'display':'block'
      })
      setTimeout(function() {
        $('.nofill').css({
          'display':'none'
        })
      },3000)
    }
  }
})

$('.moresinger').click(function() {
  $('#exampleInputEmail2').val($(this).html());
  var rsn = $(this).attr('rsn');
  searchsinger(rsn);
  $('.nofill').css({
    'display':'none'
  })
})

// 钢琴函数
  function pianodown(i) {
    $('#pianom'+i)[0].currentTime=0;
    $('#pianom'+i)[0].play();
    $('#key'+i).css({'-webkit-transform':'rotateX(-15deg) scale(0.96)'});
    $('#key'+i).css({'transform':'rotateX(-15deg) scale(0.96)'});
  };

  function pianoup(i) {
    $(i).css({'-webkit-transform':'rotateX(0deg) scale(1)'});
    $(i).css({'transform':'rotateX(0deg) scale(1)'});
  };

  var nums;
  document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    nums = e.keyCode;
    if((65<=nums&&nums<=90)||(nums==48||nums==56||nums==57)||(186<=nums&&nums<=192)||(219<=nums&&nums<=222)) {
      keychange(nums);
      pianodown(nums);
    }
  };

  document.onkeyup=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    nums = e.keyCode;
    if((65<=nums&&nums<=90)||(nums==48||nums==56||nums==57)||(186<=nums&&nums<=192)||(219<=nums&&nums<=222)) {
      keychange(nums);
      pianoup('#key'+nums);
    }
  };

  function keychange(num) {
      switch(num) {
      case 81: nums = 1;break;
      case 56: nums = 2; break;
      case 87: nums = 3; break;
      case 57: nums = 4; break;
      case 69: nums = 5; break;
      case 82: nums = 6; break;
      case 48: nums = 7; break;
      case 84: nums = 8; break;
      case 189: nums = 9; break;
      case 89: nums = 10; break;
      case 187: nums = 11; break;
      case 85: nums = 12; break;
      case 65: nums = 13; break;
      case 73: nums = 14; break;
      case 83: nums = 15; break;
      case 79: nums = 16; break;
      case 68: nums = 17; break;
      case 70: nums = 18; break;
      case 80: nums = 19; break;
      case 71: nums = 20; break;
      case 219: nums = 21; break;
      case 72: nums = 22; break;
      case 221: nums = 23; break;
      case 74: nums = 24; break;
      case 90: nums = 25; break;
      case 75: nums = 26; break;
      case 88: nums = 27; break;
      case 76: nums = 28; break;
      case 67: nums = 29; break;
      case 86: nums = 30; break;
      case 186: nums = 31; break;
      case 66: nums = 32; break;
      case 222: nums = 33; break;
      case 78: nums = 34; break;
      case 220: nums = 35; break;
      case 77: nums = 36; break;
    };
  }

//手动搜索
  function searchsinger(rsns) {
    var srsn = rsns;
    $.ajax({
      url: '../json/melody.json',
      dataType:"json",
      post:'post',
      success:function(data) {
        $('.morelist').empty();
        var tr1 = '<tr>';
        var tr2 = '<td class="singerphoto" style="background-image:url(';
        var tr3 = ');">';
        var tr4 = '<td class="singersong">';
        var tr5 = '</td>';
        var tr6 = '</tr>';
        var m=0;
        for(var i = 0; i<data[0].results[srsn].moresinger.length; i++) {
          var lengths = data[0].results[srsn].moresinger.length;
          var tr = Math.floor(lengths/3);
          var trs = lengths%3;
          var str;
          if(tr==1) {
            str=0;
          }
          if(lengths-tr>5 && trs==2) {
            str = lengths-tr-5;
          }
          if(lengths-tr>4 && trs==1) {
            str = lengths-tr-4;
          }
          if(lengths-tr>3 && trs==0) {
            str = lengths-tr-3;
          }
          if(m==0) {
            $('.morelist').append('<tr><td class="singerNames" colspan="6">'+data[0].results[srsn].moresinger[i].singer+'</td></tr>')
          }
          if(i<=tr+str) {
            $('.morelist').append(tr1+tr2+data[0].results[srsn].moresinger[i].picture+tr3+tr5+tr4+data[0].results[srsn].moresinger[i].music+tr5+tr2+data[0].results[srsn].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[srsn].moresinger[i].music+tr5+tr2+data[0].results[srsn].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[srsn].moresinger[i].music+tr5+tr6);
            m++;
          }
          else if(trs==2) {
            $('.morelist').append(tr1+tr2+data[0].results[srsn].moresinger[i].picture+tr3+tr5+tr4+data[0].results[srsn].moresinger[i].music+tr5+tr2+data[0].results[srsn].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[srsn].moresinger[i].music+tr5+tr6);
            m=0;
          }
          else if(trs==1) {
            $('.morelist').append(tr1+tr2+data[0].results[srsn].moresinger[i].picture+tr3+tr5+tr4+data[0].results[srsn].moresinger[i].music+tr5+tr6);
            m=0;
          }
        }
      }
    })
  }

// 鼠标滚动加载
  function moreandmore(FC) {
    var fcs = FC;
    $.ajax({
      url: '../json/melody.json',
      dataType:"json",
      post:'post',
      success:function(data) {
        var tr1 = '<tr>';
        var tr2 = '<td class="singerphoto" style="background-image:url(';
        var tr3 = ');">';
        var tr4 = '<td class="singersong">';
        var tr5 = '</td>';
        var tr6 = '</tr>';
        var m=0
        //更多歌手
       if(fcs>data[0].results.length-1) {
        if($('.nomore').html()==null || $('.nomore').html()=='') {
          $('.morelist').append('<tr><td class="singerNames nomore" colspan="6">没有更多歌手了</td></tr>')
        }
       }
       else {
        for(var i = 0; i<data[0].results[fcs].moresinger.length; i++) {
          var lengths = data[0].results[fcs].moresinger.length;
          var tr = Math.floor(lengths/3);
          var trs = lengths%3;
          var str;
          if(tr==1) {
            str=0;
          }
          if(lengths-tr>5 && trs==2) {
            str = lengths-tr-5;
          }
          if(lengths-tr>4 && trs==1) {
            str = lengths-tr-4;
          }
          if(lengths-tr>3 && trs==0) {
            str = lengths-tr-3;
          }
          if(m==0) {
            $('.morelist').append('<tr><td class="singerNames" colspan="6">'+data[0].results[fcs].moresinger[i].singer+'</td></tr>')
          }
          if(i<=tr+str) {
            $('.morelist').append(tr1+tr2+data[0].results[fcs].moresinger[i].picture+tr3+tr5+tr4+data[0].results[fcs].moresinger[i].music+tr5+tr2+data[0].results[fcs].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[fcs].moresinger[i].music+tr5+tr2+data[0].results[fcs].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[fcs].moresinger[i].music+tr5+tr6);
            m++;
          }
          else if(trs==2) {
            $('.morelist').append(tr1+tr2+data[0].results[fcs].moresinger[i].picture+tr3+tr5+tr4+data[0].results[fcs].moresinger[i].music+tr5+tr2+data[0].results[fcs].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[fcs].moresinger[i].music+tr5+tr6);
            m=0;
          }
          else if(trs==1) {
            $('.morelist').append(tr1+tr2+data[0].results[fcs].moresinger[i].picture+tr3+tr5+tr4+data[0].results[fcs].moresinger[i].music+tr5+tr6);
            m=0;
          }
        };
       }
      }
    })
  }

// 正常加载
  function moremusic() {
    $.ajax({
      url: '../json/melody.json',
      dataType:"json",
      post:'post',
      success:function(data) {
        $('.morelist').empty();
        var tr1 = '<tr>';
        var tr2 = '<td class="singerphoto" style="background-image:url(';
        var tr3 = ');">';
        var tr4 = '<td class="singersong">';
        var tr5 = '</td>';
        var tr6 = '</tr>';
        var m=0
        for(var nums = 0; nums<3; nums++) {
        for(var i = 0; i<data[0].results[nums].moresinger.length; i++) {
          var lengths = data[0].results[nums].moresinger.length;
          var tr = Math.floor(lengths/3);
          var trs = lengths%3;
          var str;
          if(tr==1) {
            str=0;
          }
          if(lengths-tr>5 && trs==2) {
            str = lengths-tr-5;
          }
          if(lengths-tr>4 && trs==1) {
            str = lengths-tr-4;
          }
          if(lengths-tr>3 && trs==0) {
            str = lengths-tr-3;
          }
          if(m==0) {
            $('.morelist').append('<tr><td class="singerNames" colspan="6">'+data[0].results[nums].moresinger[i].singer+'</td></tr>')
          }
          if(i<=tr+str) {
            $('.morelist').append(tr1+tr2+data[0].results[nums].moresinger[i].picture+tr3+tr5+tr4+data[0].results[nums].moresinger[i].music+tr5+tr2+data[0].results[nums].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[nums].moresinger[i].music+tr5+tr2+data[0].results[nums].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[nums].moresinger[i].music+tr5+tr6);
            m++;
          }
          else if(trs==2) {
            $('.morelist').append(tr1+tr2+data[0].results[nums].moresinger[i].picture+tr3+tr5+tr4+data[0].results[nums].moresinger[i].music+tr5+tr2+data[0].results[nums].moresinger[++i].picture+tr3+tr5+tr4+data[0].results[nums].moresinger[i].music+tr5+tr6);
            m=0;
          }
          else if(trs==1) {
            $('.morelist').append(tr1+tr2+data[0].results[nums].moresinger[i].picture+tr3+tr5+tr4+data[0].results[nums].moresinger[i].music+tr5+tr6);
            m=0;
          }
        }
       }
      }
    })
  }
})

var alltime = 0;
function musicprograss() {
  alltime+=100/36;
  $('.progress-bar').css({
    'width': alltime+'%'
  });
  $('.progress-bar').html(Math.round(alltime)+'%');
  if(Math.round(alltime) == 100) {
    setInterval(function() {
      $('.loading').css({
        'display':'none'
      });
    },2000);
  }
}

